/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.view.grid.editor.impl;
//
//import java.awt.Component;
//import java.awt.Dimension;
//import java.awt.GridBagLayout;
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.Enumeration;
//import java.util.List;
//
//import javax.swing.BorderFactory;
//import javax.swing.JLabel;
//import javax.swing.JTree;
//import javax.swing.SwingConstants;
//import javax.swing.SwingUtilities;
//import javax.swing.tree.DefaultMutableTreeNode;
//import javax.swing.tree.DefaultTreeModel;
//import javax.swing.tree.TreeNode;
//import javax.swing.tree.TreePath;
//
//import com.reportgear.core.data.SalaryHelper;
//import com.reportgear.core.data.model.SalaryDataObject;
//import com.reportgear.core.swing.layout.LayoutHelper;
//import com.reportgear.core.swing.pane.base.EditorPane;
//import com.reportgear.report.context.ReportContext;
//import com.reportgear.report.model.SalaryReportModel;
//import com.reportgear.report.model.cell.value.salary.SalaryMainObject;
//import com.reportgear.report.model.cell.value.salary.SalaryObjectType;
//import com.reportgear.report.model.cell.value.support.CommonObject;
//import com.reportgear.report.view.JWorksheet;
//import com.reportgear.swing.controls.JTreeComboBox;
//
///**
// * 工资定制编辑面板
// * 
// * @version 1.0 2011-3-26
// * @author <a herf="lexloo@gmail.com">lexloo</a>
// * @since ANNCSR 2.0
// */
//public class SalaryMainObjectEditPane extends EditorPane<SalaryMainObject> {
//	/**
//	 * UID
//	 */
//	private static final long serialVersionUID = 5268678506837385744L;
//	private JTreeComboBox cbxItem = new JTreeComboBox();
//	private SalaryMainObject smo;
//	private SalaryObjectType sot;
//
//	public SalaryMainObjectEditPane() {
//		this.setBorder(BorderFactory.createEtchedBorder());
//		this.setLayout(new GridBagLayout());
//
//		JWorksheet sheet = ReportContext.INSTANCE.getCurrentWorkSheet();
//		SalaryReportModel model = (SalaryReportModel) sheet.getReportModel();
//		this.sot = model.getObjectType();
//		
//		this.add(new JLabel(this.sot.getName(), SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(
//				0, 0, 1, 1));
//		this.add(cbxItem, LayoutHelper.newGridBagConstraints(1, 0, 1, 1));
//		cbxItem.setPreferredSize(new Dimension(300, 24));
//
//		this.refreshItems(this.sot);
//	}
//
//	/**
//	 * 更新薪酬项目
//	 * 
//	 * @param objectType
//	 *            对象id
//	 */
//	private void refreshItems(SalaryObjectType objectType) {
//		List<CommonObject> items = this.loadItems(objectType);
//
//		DefaultMutableTreeNode root = new DefaultMutableTreeNode(objectType.getName());
//		DefaultMutableTreeNode pNode = null;
//
//		for (CommonObject o : items) {
//			DefaultMutableTreeNode node = new DefaultMutableTreeNode(o);
//			if ((pNode != null) && ((CommonObject) pNode.getUserObject()).getId() == o.getParentId()) {
//				pNode.add(node);
//			} else {
//				pNode = this.searchParentNode(root, o);
//				if (pNode == null) {
//					root.add(node);
//				} else {
//					pNode.add(node);
//				}
//			}
//		}
//
//		JTree tree = new JTree(root);
//		cbxItem.setTree(tree);
//	}
//
//	/**
//	 * 加载发放表对象信息
//	 * 
//	 * @param objectType
//	 *            对象类型
//	 * @return 对象信息
//	 */
//	private List<CommonObject> loadItems(SalaryObjectType objectType) {
//		List<SalaryDataObject> sdoList;
//
//		if (objectType == SalaryObjectType.ORGANIZATION) {
//			sdoList = SalaryHelper.INSTANCE.getOrgs();
//		} else {
//			sdoList = SalaryHelper.INSTANCE.getCenters();
//		}
//
//		List<CommonObject> items = new ArrayList<CommonObject>();
//		for (SalaryDataObject sdo : sdoList) {
//			items.add(new CommonObject(sdo.getInt("id"), sdo.getInt("parentId"), sdo.getString("code"), sdo
//					.getString("name")));
//		}
//
//		Collections.sort(items);
//
//		return items;
//	}
//
//	@SuppressWarnings("unchecked")
//	private DefaultMutableTreeNode searchParentNode(TreeNode root, CommonObject co) {
//		int pid = co.getParentId();
//		DefaultMutableTreeNode node = null;
//		DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) root;
//
//		Enumeration e = rootNode.breadthFirstEnumeration();
//		while (e.hasMoreElements()) {
//			node = (DefaultMutableTreeNode) e.nextElement();
//			Object o = node.getUserObject();
//			if (o instanceof CommonObject) {
//				if (((CommonObject) o).getId() == pid) {
//					return node;
//				}
//			}
//		}
//
//		return null;
//	}
//
//	@SuppressWarnings("unchecked")
//	private DefaultMutableTreeNode searchNode(TreeNode root, CommonObject co) {
//		int id = co.getId();
//		DefaultMutableTreeNode node = null;
//		DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) root;
//		Enumeration e = rootNode.breadthFirstEnumeration();
//		while (e.hasMoreElements()) {
//			node = (DefaultMutableTreeNode) e.nextElement();
//			Object o = node.getUserObject();
//			if (o instanceof CommonObject) {
//				if (((CommonObject) o).getId() == id) {
//					return node;
//				}
//			}
//		}
//		return null;
//	}
//
//	@Override
//	public void populate(SalaryMainObject value) {
//		this.smo = value;
//
//		if (value.getObject().getId() <= 0) {
//			return;
//		}
//
//		JTree tree = cbxItem.getTree();
//		DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
//		DefaultMutableTreeNode node = this.searchNode((TreeNode) treeModel.getRoot(), value.getObject());
//
//		if (node == null) {
//			return;
//		}
//
//		TreePath SelectedPath = new TreePath(treeModel.getPathToRoot(node));
//		cbxItem.setSelectedItem(SelectedPath);
//		tree.makeVisible(SelectedPath);
//	}
//
//	@Override
//	public int showDialog(Component owner) {
//		return this.showDialog(SwingUtilities.getWindowAncestor(owner), this.sot.getName(), 460, 140);
//	}
//
//	@Override
//	public SalaryMainObject update() {
//		DefaultMutableTreeNode selectNode = cbxItem.getSelectedTreeNode();
//		this.smo.getObject().copyProperties((CommonObject) selectNode.getUserObject());
//
//		return this.smo;
//	}
//}
